//
//  HomeMainController.m
//  EasyToTesco2.0
//
//  Created by 杨森 on 2017/5/15.
//  Copyright © 2017年 EasyToTesco. All rights reserved.
//

#import "HomeMainController.h"
#import "FirstHomeCell.h"
#import "homeCollectionViewCell.h"
#import "CenterMenuView.h"
#import "CLHomeHeader.h"
#import "HomeRecommendCell.h"
#import "HomePlayCell.h"
#import "HomeCarCell.h"
#import "HomeTourCell.h"
#import "HomeRankingCell.h"
#import "HomeListCell.h"
#import "HomeFarmingCell.h"

#import "CustNaviBarView.h"

#import "EasyNewsModel.h"
#import "GoodListsModel.h"

#import "EasyToTescoNewsController.h"
#import "RecommendedController.h"
#import "ActViewController.h"
#import "RechargeController.h"
#import "DervativeAreaController.h"


static NSString * const HOME_Recommend          = @"HOME_RecommendIdentifier";
static NSString * const HOME_Play               = @"HOME_PlayIdentifier";
static NSString * const HOME_Car                = @"HOME_CarIdentifier";
static NSString * const HOME_Tour               = @"HOME_TourIdentifier";
static NSString * const HOME_Ranking            = @"HOME_RankingIdentifier";
static NSString * const HOME_List               = @"HOME_ListIdentifier";
static NSString * const HOME_Farming            = @"HOME_FarmingIdentifier";


#define contenViewH  (HEIGHT/4)+(HEIGHT/4+12)+(30+1)+8

@interface HomeMainController ()<UIScrollViewDelegate,CenterMenuViewDelegate,UITableViewDelegate,UITableViewDataSource>
{
    UIView *alphaView;
}

@property(nonatomic,strong)CenterMenuView     *menuView;
@property(nonatomic,strong)SDCycleScrollView  *cycleScrollView;
@property(nonatomic,strong)CLHomeHeader       *newsView;
@property(nonatomic,strong)UIView             *lineView;

@property(nonatomic,weak)UIImageView          *barImageView;

/**
 *  轮播数组
 */
@property(nonatomic,strong)NSArray            *cycleUrlArray;
/**
 *  亿乐快报
 */
@property(nonatomic,strong)NSMutableArray     *EasyNewsArray;
@property(nonatomic,strong)NSMutableArray     *goodsListArray;
@property (nonatomic)CGFloat                  halfHeight;

@property(nonatomic,strong)UITableView        *tableView;
@property(nonatomic,strong)CustNaviBarView    *naviBarView;

@end

@implementation HomeMainController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self buildHomeView];
}

- (void)buildHomeView
{
    self.title = @"首页";
    [self.view addSubview:self.tableView];
    
    self.automaticallyAdjustsScrollViewInsets = false;
 
    [self.tableView registerClass:[HomeRecommendCell class] forCellReuseIdentifier:HOME_Recommend];
    [self.tableView registerClass:[HomePlayCell class] forCellReuseIdentifier:HOME_Play];
    [self.tableView registerClass:[HomeCarCell class] forCellReuseIdentifier:HOME_Car];
    [self.tableView registerClass:[HomeTourCell class] forCellReuseIdentifier:HOME_Tour];
    [self.tableView registerClass:[HomeRankingCell class] forCellReuseIdentifier:HOME_Ranking];
    [self.tableView registerClass:[HomeListCell class] forCellReuseIdentifier:HOME_List];
    [self.tableView registerClass:[HomeFarmingCell class] forCellReuseIdentifier:HOME_Farming];
    
    self.tableView.contentInset = UIEdgeInsetsMake(0,0,48,0);

    [self.view addSubview:self.naviBarView];
}

#pragma mark request data



#pragma mark - scrollView delegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    CGFloat y = scrollView.contentOffset.y;
    if(y <= 0){
        _naviBarView.alpha = 1;//往上滑动
        _naviBarView.backgroundColor = [UIColor clearColor];
    }else{
        if (y/100 >= 1){
            return;
        }
        [UIView animateWithDuration:0.2f animations:^{
            _naviBarView.backgroundColor = [UIColor purpleColor];
        }];
        _naviBarView.alpha = (y)/100;
    }
}


#pragma mark tableView datasource delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 7;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    switch (section) {
        case 6:
            return 10;
            break;
            
        default:
            return 1;
            break;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    switch (indexPath.section) {
        case 0:
        {
            HomeRecommendCell *cell = [tableView dequeueReusableCellWithIdentifier:HOME_Recommend forIndexPath:indexPath];
            return cell;
        }
            break;
            
        case 1:
        {
            HomePlayCell *cell = [tableView dequeueReusableCellWithIdentifier:HOME_Play forIndexPath:indexPath];
            return cell;
        }
            break;
            
        case 2:
        {
            HomeCarCell *cell = [tableView dequeueReusableCellWithIdentifier:HOME_Car forIndexPath:indexPath];
            return cell;
        }
            break;
            
        case 3:
        {
            HomeTourCell *cell = [tableView dequeueReusableCellWithIdentifier:HOME_Tour forIndexPath:indexPath];
            return cell;
        }
            break;
            
        case 4:
        {
            HomeFarmingCell *cell = [tableView dequeueReusableCellWithIdentifier:HOME_Farming forIndexPath:indexPath];
            return cell;
        }
            break;

            
        case 5:
        {
            HomeRankingCell *cell = [tableView dequeueReusableCellWithIdentifier:HOME_Ranking forIndexPath:indexPath];
            return cell;
        }
            break;
        
        default:
        {
            HomeListCell *cell = [tableView dequeueReusableCellWithIdentifier:HOME_List forIndexPath:indexPath];
            return cell;
        }
            break;
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.section) {
        case 0:
            return 190;
            break;
        case 1:
            return 190;
            break;
        case 4:
            return (WIDTH-45)/4*2+15 + 40;
            break;
        case 5:
            return 310;
            break;
        case 6:
            return HEIGHT/4;
            break;
        default:
            return (WIDTH-50)/3*2+20 + 40;  //2.3
            break;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        UIView *contenView = [[UIView alloc] initWithFrame:CGRectMake(0,0,WIDTH,contenViewH)];
        [contenView addSubview:self.cycleScrollView];
        [contenView addSubview:self.menuView];
        [contenView addSubview:self.newsView];
        [contenView addSubview:self.lineView];
        return contenView;
    }
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    
    switch (section) {
        case 0:
            return contenViewH;
            break;
        case 5:
            return 0.001;
            break;
        case 6:
            return 0.001;
            break;
        default:
            return 8;
            break;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.001;
}

#pragma mark view delegate
- (void)homeMenuDidSelectedAtIndex:(NSInteger)index
{
    NSInteger choose = index-10;
    
    switch (choose) {
        case 0:
        {
            
        }
            break;
            
        case 1:
        {
            RecommendedController*recommendedVC = [[RecommendedController alloc] init];
            recommendedVC.title = @"农特产品";
            recommendedVC.is_Agricultural = YES;
            [self.navigationController pushForm:self.navigationController pushToAndHidden:recommendedVC];
        }
            break;
            
        case 2:
        {
            RecommendedController*recommendedVC = [[RecommendedController alloc] init];
            recommendedVC.title = @"亿乐特惠";
            recommendedVC.is_teHui = YES;
            [self.navigationController pushForm:self.navigationController pushToAndHidden:recommendedVC];
        }
            break;
            
        case 3:
        {
            ActViewController *ActViewVc = [[ActViewController alloc] init];
            ActViewVc.title = @"活动专区";
            ActViewVc.homePush = YES;
            [self.navigationController pushForm:self.navigationController pushToAndHidden:ActViewVc];
        }
            break;
            
        case 4:
        {
            RechargeController *rechargeVC = [[RechargeController alloc] init];
            rechargeVC.title = @"话费充值";
            [self.navigationController pushForm:self.navigationController pushToAndHidden:rechargeVC];
        }
            break;
            
        case 5:
        {
            
        }
            break;
            
        case 6:
        {
            DervativeAreaController *DervativeAreaVC = [[DervativeAreaController alloc] init];
            DervativeAreaVC.title = @"微商专区";
            DervativeAreaVC.value = @"";
            [self.navigationController pushForm:self.navigationController pushToAndHidden:DervativeAreaVC];
        }
            break;
            
        case 7:
        {
            RecommendedController*recommendedVC = [[RecommendedController alloc] init];
            recommendedVC.title = @"精品推荐";
            [self.navigationController pushForm:self.navigationController pushToAndHidden:recommendedVC];

        }
            break;
            
        default:
            break;
    }
}


#pragma mark view 周期
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:false animated:animated];
}

#pragma mark lazy
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,0,WIDTH,HEIGHT) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
//        _tableView.backgroundColor = BACKGROUND_COLOR;
    }
    return _tableView;
}


- (SDCycleScrollView *)cycleScrollView
{
    if (!_cycleScrollView) {
        _cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0,0,WIDTH,HEIGHT/4) delegate:nil placeholderImage:[UIImage imageNamed:@"homeCycleLoad.jpg"]];
        _cycleScrollView.pageControlAliment = SDCycleScrollViewPageContolAlimentRight;
        _cycleScrollView.imageURLStringsGroup = @[
                                                  @"http://liveimg.9158.com/pic/ad/20170511/20170511124304912.png",
                                                  @"http://liveimg.9158.com/pic/ad/20170503/20170503170732620.jpg"
                                                  ];
        _cycleScrollView.pageDotColor = [UIColor whiteColor];
        _cycleScrollView.currentPageDotColor = DEFAULT_COLOR;
    }
    return _cycleScrollView;
}

- (CenterMenuView *)menuView
{
    if (!_menuView) {
        _menuView = [[CenterMenuView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.cycleScrollView.frame), WIDTH, HEIGHT/4+12) MenuArr:self.menuTitleArray];
        _menuView.backgroundColor = [UIColor whiteColor];
        _menuView.delegate = self;
    }
    return _menuView;
}


- (NSArray <NSString *>*)menuTitleArray
{
    return @[@"热门景点",@"农特产品",@"亿乐特惠",@"活动专区",@"话费充值",@"油卡充值",@"微商专区",@"精品推荐"];
}

- (CLHomeHeader *)newsView
{
    if (!_newsView) {
        _newsView = [[CLHomeHeader alloc] initWithFrame:CGRectMake(0,CGRectGetMaxY(_menuView.frame)+1,WIDTH,30)];
        _newsView.newses = @[@[@"关于“亿乐Go 春风行动”的公告"],@[@"关于2017年五一放假的通知"]];
        WeakSelf(self);
        _newsView.cliclCelllable1 = ^{
            [weakself pushToEasyToTescoNewsControlller];
        };
        _newsView.cliclCelllable2 = ^{
            [weakself pushToEasyToTescoNewsControlller];
        };
        
    }
    return _newsView;
}

- (void)pushToEasyToTescoNewsControlller
{
    EasyToTescoNewsController *EasyToTescoNewsVc = [[EasyToTescoNewsController alloc] init];
    EasyToTescoNewsVc.title = @"亿乐快报";
    [self.navigationController pushForm:self.navigationController pushToAndHidden:EasyToTescoNewsVc];
}

- (UIView *)lineView
{
    if (!_lineView) {
        _lineView = [[UIView alloc] initWithFrame:CGRectMake(0,CGRectGetMaxY(_newsView.frame), WIDTH,8)];
        _lineView.backgroundColor = BACKGROUND_COLOR;
    }
    return _lineView;
}

- (CustNaviBarView *)naviBarView
{
    if (!_naviBarView) {
        _naviBarView = [[CustNaviBarView alloc] initWithFrame:CGRectMake(0,0,WIDTH,64)];
        _naviBarView.backgroundColor = [UIColor clearColor];
//        _naviBarView.alpha = ;
        
    }
    return _naviBarView;
}


@end
